package com.xxs.utils;

import javax.servlet.http.HttpSession;

import com.xxs.common.Const;
import com.xxs.utils.xml.XmlDom4jUtils;

public abstract class Context {
	
	private static final String CURRENT_SITE_ID = "siteId";
	private static final String CURRENT_SITE_NAME = "siteName";
	private static final String CURRENT_SITE_CODE = "siteCode";
	private static final String CURRENT_SITE_DOMAIN = "siteDomain";
	
	public static String getCurrentSiteId(HttpSession session) {
		String siteId = (String) session.getAttribute(CURRENT_SITE_ID);
		return siteId != null ? siteId : null;
	}
	
	public static String getCurrentSiteName(HttpSession session, String siteId) {
		return XmlDom4jUtils.getParaValueByAttrAndValue(Const.SITE_PATH,"site","id",siteId,"name");
	}
	
	public static String getCurrentSiteCode(HttpSession session, String siteId) {
		return XmlDom4jUtils.getParaValueByAttrAndValue(Const.SITE_PATH,"site","id",siteId,"code");
	}
	
	public static String getCurrentSiteDomain(HttpSession session, String siteId) {
		return XmlDom4jUtils.getParaValueByAttrAndValue(Const.SITE_PATH,"site","id",siteId,"domain");
	}
	
	public static void setCurrentSiteId(HttpSession session, String siteId) {
		session.setAttribute(CURRENT_SITE_ID, siteId);
	}
	
	public static void setCurrentSiteName(HttpSession session, String siteId) {
		String siteName = XmlDom4jUtils.getParaValueByAttrAndValue(Const.SITE_PATH,"site","id",siteId,"name");
		session.setAttribute(CURRENT_SITE_NAME, siteName);
	}
	
	public static void setCurrentSiteCode(HttpSession session, String siteId) {
		String siteCode = XmlDom4jUtils.getParaValueByAttrAndValue(Const.SITE_PATH,"site","id",siteId,"code");
		session.setAttribute(CURRENT_SITE_CODE, siteCode);
	}

	public static void resetCurrentSiteId(HttpSession session) {
		session.removeAttribute(CURRENT_SITE_ID);
	}

	/**
	 * 站点线程变量（页数）
	 */
	private static ThreadLocal<String> siteHolder = new ThreadLocal<String>();

	public static void setCurrentSiteId(String siteId) {
		siteHolder.set(siteId);
	}

	public static String getCurrentSiteId() {
		return siteHolder.get();
	}

	public static void resetCurrentSiteId() {
		siteHolder.remove();
	}

}
